package com.myframe.demo.servlet;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

public class WordExpServlet extends HttpServlet{

	private String path="";
	private String fileName="";
	private String filePath="";
	private String fileNameCn="";
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		
		String id = req.getParameter("id");
		
	    path = req.getRealPath("/")+"tmp\\";
		fileName = "--"+ ".doc";
	    filePath = path+fileName;
	    
		System.out.println("filePath==="+filePath);
		
		
	}

	/**
	 * 通过自然人
	 * @param req
	 * @param res
	 * @param vo
	 */
	public void printTgzzr(HttpServletRequest req, HttpServletResponse res){
		fileNameCn="预审通过通知书";
		Document document = new Document(PageSize.A4);
		try { 
			RtfWriter2.getInstance(document,new FileOutputStream(filePath)); 
			BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
			Font titleFont = new Font(bfChinese, 20, Font.NORMAL, Color.BLACK); 
			Font parFont = new Font(bfChinese, 11, Font.NORMAL, Color.BLACK); 
			parFont.setStyle("line-height: 200%;");
			Font tableFont = new Font(bfChinese, 11, Font.NORMAL, Color.BLACK); 
			Font tableFont1 = new Font(bfChinese, 12, Font.BOLD, Color.BLACK); 
			document.open();
			
			int width1[] = { 3, 10, 11, 10, 10, 10, 13}; 
			Paragraph par = new Paragraph("",titleFont);
			par.setAlignment(1);
			document.add(par);
			Paragraph title1 = new Paragraph("四川省人民政府政务服务中心部门服务事项",titleFont);
			title1.setLeading(1.5f);
			title1.setAlignment(1);
			document.add(title1);
			document.add(par);
			Paragraph title2 = new Paragraph("预审通过通知书",titleFont);
			title2.setAlignment(1);
			document.add(title2);
			

			
			Table table = new Table(7,10);
			table.setBorderWidth(1);
			table.setBorderColor(Color.BLACK);
			table.setPadding(10);
			table.setSpacing(0);
			table.setOffset(4f);
			table.setAlignment(Element.ALIGN_MIDDLE);
			table.setAlignment(Element.ALIGN_CENTER);
		
			table.setWidth(97);
			table.setWidths(width1);
			/*table.setUseVariableBorders(true);
			table.setAutoFillEmptyCells(true);*/
			
			Cell cell = new Cell(new Paragraph("申请编号：",tableFont1));
			cell.setColspan(2);
			cell.setBorder(0);
			cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			table.addCell(cell);
			
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setBorder(0);
			cell.setColspan(3);
			table.addCell(cell);
			
			cell = new Cell(new Paragraph(" 申请日期：",tableFont1));
			cell.setBorder(0);
			//cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			table.addCell(cell);
			
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setBorder(0);
			table.addCell(cell);
			
			
			cell = new Cell(new Paragraph("申请事项",tableFont1));
			cell.setColspan(2);
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			cell.setMaxLines(5); 
			table.addCell(cell);
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setVerticalAlignment(1);
			cell.setColspan(5);
			table.addCell(cell);
			
			cell = new Cell(new Paragraph("申请人",tableFont1));
			cell.setRowspan(2);
			//cell.setWidth(1);
			cell.setUseDescender(true);
			cell.setHorizontalAlignment(1);
			cell.setVerticalAlignment(1);
			table.addCell(cell);
			
			cell = new Cell(new Paragraph("姓　　名",tableFont1));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("电话",tableFont1));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("电子信箱",tableFont1));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("通讯地址",tableFont1));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setVerticalAlignment(1);
			cell.setColspan(3);
			table.addCell(cell);
			cell = new Cell(new Paragraph("邮政编码",tableFont1));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			
			cell = new Cell(new Paragraph("受理窗口",tableFont1));
			cell.setRowspan(4);
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			
			
			cell = new Cell(new Paragraph("预审单位",tableFont1));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("--",tableFont));
			
			cell.setVerticalAlignment(1);
			cell.setColspan(3);
			table.addCell(cell);
			cell = new Cell(new Paragraph("预审件数",tableFont1));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("--",tableFont));
			cell.setVerticalAlignment(1);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);



			
			cell = new Cell(new Paragraph("申请人认可签字",tableFont1));
			cell.setColspan(2);
			cell.setHorizontalAlignment(1);
			table.addCell(cell);
			cell = new Cell(new Paragraph("",tableFont));
			cell.setColspan(5);
			table.addCell(cell);
			
			document.add(table);
			
			
			
			document.add(new Paragraph("注：该通知书是网上预审通过的凭证，申请人凭此通知书到网站查询事项办理情况，持本通知书到窗口提交申请材料，可优先快速办理。",parFont));
			document.add(new Paragraph("附：已交材料清单",parFont));
			
			
			document.close();
			
			print(req,res);
		}catch (FileNotFoundException e) { 
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	public void print(HttpServletRequest req, HttpServletResponse res) throws IOException{
		
		OutputStream output = res.getOutputStream();
		byte b[] = new byte[1024];
		
		File fileLoad = new File(filePath);
		
		String docName = fileNameCn+".doc";
		docName = java.net.URLEncoder.encode(docName, "UTF-8");
		res.setHeader("Content-Disposition","attachment;filename="+ new String(docName.getBytes("UTF-8"), "GBK"));
		res.setContentType("application/doc;charset=gb2312");
		long fileLength = fileLoad.length();
		String length = String.valueOf(fileLength);
		res.setHeader("Content_Length", length);
		FileInputStream input = new FileInputStream(fileLoad);
		int n = 0;
		while ((n = input.read(b)) != -1) {
			output.write(b, 0, n);
		}
	}
}
